local jass = require 'jass.common'
local dbg  = require 'jass.debug'

--[[
    创建点作为图像的左下角位置
    该功能存在boug会在图像上和右面多比256象素.
    所以需要支持Alpa通道的囹像且上和右面最后一行
    像素为透明才能完美显示
]]

local image = {}
setmetatable(image,image)
ac.image = image

image.allimages = { }

image.types = {
    ['阴影'] = 0,
    ['选择'] = 1,
    ['指示器']  = 2,
    ['闭塞标志'] = 3,
    ['地面纹理变化'] = 4,
    ['最顶端'] = 5,
}

local gchash = 0

function image:__call(data)
    local img = setmetatable(data or { },self)
    img:init()
    return img 
end

local mt = {}
image.__index = mt 

-- 结构
mt.type = 'image'

-- 伪句柄
mt.handle = jass.ConvertUnitState(0)

-- 图像类型
mt.image_type = '指示器'

-- 图像路径
mt.image = 'textures\\black32.blp'

-- 尺寸
mt.size = 512.0

-- 是否显示
mt.show_flag = true 

-- 颜色
mt.red   = 255
mt.green = 255
mt.blue  = 255
mt.alpha = 255

function mt:init()
    local image_type = image.types[self.image_type]
    local handle = jass.CreateImage(self.image,self.size,self.size,self.size,0,0,0,0,0,0,image_type)
    self.handle = handle 
    jass.SetImageRenderAlways(self.handle,true)
    self:set_point(self.point or ac.point(0,0))
    self.gchash = handle
    dbg.gchash(self,handle)
    image.allimages[self] = true 
    if self.show_flag then 
        jass.ShowImage(self.handle,true)
    end 
    return self
end 

function mt:remove()
    if self.removed then 
        return 
    end 
    self.removed = true 
    local handle = self.handle 
    self:hide()
    jass.DestroyImage(handle)
    image.allimages[self] = nil 
    self.handle = nil 
end 

function mt:is_show()
    return self.show_flag
end 

function mt:show()
    self.show_flag = true 
    jass.ShowImage(self.handle,true)
end 

function mt:hide()
    self.show_flag = false 
    jass.ShowImage(self.handle,false)
end     

-- 改变图像水上状态 
-- @param flag1 水上或水下
-- @param flag2 是否受水的alpha通道影响
function mt:abovwater(flag1,flag2) 
    jass.SetImageAboveWater(self.handle,flag1,flag2)
    return self
end

-- 改变图像类型
function mt:set_image_type(name)
    local newtype = image.types[name]
    if newtype then 
        jass.SetImageType(self.handle,newtype)
    end 
    return self
end 

-- 设置位置
function mt:set_point(where)
    self.point = where
    local x,y,z = where:get()
    x = x - (self.size + 128) / 2 
    y = y - (self.size + 128) / 2 
    jass.SetImagePosition(self.handle,x,y,z)
end 

-- 设置高度
function mt:set_height(height)
    jass.SetImageConstantHeight(self.handle,true,height or 0)
end 

-- 设置颜色 
-- 0 params get r,g,b,a
-- 1 params change color by 0xffffffff
-- 4 params change color by r,g,b,a
function mt:color(...)
    local n = select('#',...)
    if n == 0 then 
        return self.red,self.green,self.blue,self.alpha
    end
    local args = {...}
    if n == 1 then
        self.red   = ((args[1] << 40) >> 56) 
        self.green = ((args[1] << 48) >> 56) 
        self.blue  = ((args[1] << 56) >> 56) 
        self.alpha = ((args[1] << 32) >> 56) 
    else
        self.red   = args[1] or self.red
        self.green = args[2] or self.green
        self.blue  = args[3] or self.blue
        self.alpha = args[4] or self.alpha
    end
    jass.SetImageColor(self.handle, self.red, self.green, self.blue, self.alpha)
end 

return image 